/*!
 * @file moduleSet.cpp
 * @brief Ciało moduleSet.hh
 */

#include "engine/set/moduleSet.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

moduleSet::moduleSet() :
	moduleBase("set") {
}

/*--------------------------------------------------------------------------*/

bool moduleSet::exists(string name) {
	for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
			!= this->variablesStack.end(); ++Iter) {

		if ((*Iter).first == name) {
			return true;
		}
	}
	return false;
}

/*--------------------------------------------------------------------------*/

setContainer *moduleSet::get(string name) {
	for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
			!= this->variablesStack.end(); ++Iter) {

		if ((*Iter).first == name) {
			return Iter->second;
		}
	}

	this->variablesStack.push_back(pair<string, setContainer*> (name, new setContainer()));
	return this->variablesStack.end()->second;
}

/*--------------------------------------------------------------------------*/

setContainer *moduleSet::put(string name, int val) {
	if (this->exists(name)) {
		for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
				!= this->variablesStack.end(); ++Iter) {

			if ((*Iter).first == name) {
				(*Iter).second->set(val);
				return (*Iter).second;
			}
		}
	} else {
		setContainer *tmp = new setContainer(val);
		this->variablesStack.push_back(pair<string, setContainer*> (name, tmp));
		return tmp;
	}
}

/*--------------------------------------------------------------------------*/

setContainer *moduleSet::put(string name, string val) {
	if (this->exists(name)) {
		for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
				!= this->variablesStack.end(); ++Iter) {

			if ((*Iter).first == name) {
				(*Iter).second->set(val);
				return (*Iter).second;
			}
		}
	} else {
		setContainer *tmp = new setContainer(val);
		this->variablesStack.push_back(pair<string, setContainer*> (name, tmp));
		return tmp;
	}

	return NULL;
}

/*--------------------------------------------------------------------------*/

setContainer *moduleSet::put(string name, float val) {
	if (this->exists(name)) {
		for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
				!= this->variablesStack.end(); ++Iter) {

			if ((*Iter).first == name) {
				(*Iter).second->set(val);
				return (*Iter).second;
			}
		}
	} else {
		setContainer *tmp = new setContainer(val);
		this->variablesStack.push_back(pair<string, setContainer*> (name, tmp));
		return tmp;
	}

	return NULL;
}

/*--------------------------------------------------------------------------*/

std::string moduleSet::listAll() {

	stringstream ss;

	for (list<pair<string, setContainer*> >::iterator Iter = this->variablesStack.begin(); Iter
			!= this->variablesStack.end(); ++Iter) {
		ss << (*Iter).first << " = " << (*Iter).second->s << endl;
	}

	return ss.str();

}

/*--------------------------------------------------------------------------*/
